package gen.kaynak.navitrack.messageProcessState;

public class MessageProcessStateManager {
	private ACMessageProcessState state;

	private static final String strMinus = "-";
	private static final String strPlus = "+";
	private static final String strLatitude = "Latitude";
	private static final String strLongitude = "Longitude";

	private String strNowProcessing = "Latitude";

	private StringBuilder sbLat = new StringBuilder();
	private StringBuilder sbLatMinutes = new StringBuilder();
	private StringBuilder sbLatSeconds = new StringBuilder();
	private String strLat = "";
	private String strLatSign = "";
	private float sngLatSingle = 0;

	private StringBuilder sbLng = new StringBuilder();
	private StringBuilder sbLngMinutes = new StringBuilder();
	private StringBuilder sbLngSeconds = new StringBuilder();
	private String strLng = "";
	private String strLngSign = "";
	private float sngLngSingle = 0;

	private String mId;

	public MessageProcessStateManager(String mIdi) {
		state = Initial.Instance();
		mId = mIdi;
	}

	public void setSign(String sign) {
		switch (strNowProcessing) {
		case strLatitude:
			strLatSign = sign;
		case strLongitude:
			strLngSign = sign;
		}
	}

	public boolean alternateProcessing() {
		boolean booAlternateProcessingTmp = false;
		switch (strNowProcessing) {
		case strLatitude:
			strNowProcessing = "Longitude";
			booAlternateProcessingTmp = true;
		case strLongitude:
			booAlternateProcessingTmp = false;
		}
		return booAlternateProcessingTmp;
	}

	public String getNowProcessing() {
		return strNowProcessing;
	}

	public String getLatSign() {
		return strLatSign;
	}

	public boolean signIsNotAssigned() {
		boolean booSignIsNotAssigned = false;
		switch (strNowProcessing) {
		case strLatitude:
			booSignIsNotAssigned = !((strLatSign.equals(strMinus)) || (strLatSign
					.equals(strPlus)));
		case strLongitude:
			booSignIsNotAssigned = !((strLngSign.equals(strMinus)) || (strLngSign
					.equals(strPlus)));
		}
		return booSignIsNotAssigned;
	}

	public String getLngSign() {
		return strLngSign;
	}

	public void appendChar(char chrAppend) {
		switch (strNowProcessing) {
		case strLatitude:
			sbLat.append(chrAppend);
		case strLongitude:
			sbLng.append(chrAppend);
		}
	}

	public void appendMinutes(char chrAppend) {
		switch (strNowProcessing) {
		case strLatitude:
			sbLatMinutes.append(chrAppend);
		case strLongitude:
			sbLngMinutes.append(chrAppend);
		}
	}

	public void calculateMinutes() {
		Float sngLatSingleTmp;
		Float sngLngSingleTmp;

		switch (strNowProcessing) {
		case strLatitude:
			sngLatSingleTmp = new Float(sbLatMinutes.toString().replaceAll(".",
					","));
			sngLatSingle = sngLatSingle + sngLatSingleTmp / 60;
			break;
		case strLongitude:
			sngLngSingleTmp = new Float(sbLngMinutes.toString().replaceAll(".",
					","));
			sngLngSingle = sngLngSingle + sngLngSingleTmp / 60;
			break;
		}

	}
	public void appendSeconds(char chrAppend) {
		switch (strNowProcessing) {
		case strLatitude:
			sbLatSeconds.append(chrAppend);
		case strLongitude:
			sbLngSeconds.append(chrAppend);
		}
	}

	public void calculateSeconds() {
		Float sngLatSingleTmp;
		Float sngLngSingleTmp;
		switch (strNowProcessing) {
		case strLatitude:
			sngLatSingleTmp = new Float(sbLatSeconds.toString().replaceAll(".",
					","));
			sngLatSingle = sngLatSingle + sngLatSingleTmp / 3600;
			break;
		case strLongitude:
			sngLngSingleTmp = new Float(sbLngSeconds.toString().replaceAll(".",
					","));
			sngLngSingle = sngLngSingle + sngLngSingleTmp / 3600;
			break;
		}
	}

	public void calculateCoordinate() {
		switch (strNowProcessing) {
	case "Latitude": break;
		// strLat = strLatSign & CStr(sngLatSingle +
		// CSng(sbLat.ToString.Replace(".", ","))).Replace(",", ".")
		// Dim conn As New
		// Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("NaviTrackConnectionString").ConnectionString)
		// Dim cmdU As New SqlCommand
		// cmdU.CommandType = CommandType.Text
		// cmdU.CommandText =
		// "UPDATE messageData SET latitude = @latitude WHERE message_id = @message_id"
		// cmdU.Connection = conn
		//
		// Dim latitudeUParam As New SqlParameter("@latitude",
		// SqlDbType.NVarChar, 50)
		// latitudeUParam.Value = strLat
		// cmdU.Parameters.Add(latitudeUParam)
		//
		// Dim message_idUParam As New SqlParameter("@message_id",
		// SqlDbType.UniqueIdentifier)
		// Dim gmessage_id As New Guid(mId)
		// message_idUParam.Value = gmessage_id
		// cmdU.Parameters.Add(message_idUParam)
		//
		// Dim previousConnectionStateU As ConnectionState = conn.State
		// Try
		// If conn.State = ConnectionState.Closed Then
		// conn.Open()
		// End If
		// cmdU.ExecuteScalar()
		// Catch ex As SqlException
		// Dim errorMessages As New StringBuilder()
		// Dim i As Integer
		// For i = 0 To ex.Errors.Count - 1
		// errorMessages.Append("<BR> Index #" & i.ToString() &
		// ControlChars.NewLine _
		// & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
		// & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
		// & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
		// & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
		// Next i
		//
		// Finally
		// If previousConnectionStateU = ConnectionState.Closed Then
		// conn.Close()
		// End If
		// End Try
		case "Longitude": break;
		// strLng = strLngSign & CStr(sngLngSingle +
		// CSng(sbLng.ToString.Replace(".", ","))).Replace(",", ".")
		// Dim conn As New
		// Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("NaviTrackConnectionString").ConnectionString)
		// Dim cmdU As New SqlCommand
		// cmdU.CommandType = CommandType.Text
		// cmdU.CommandText =
		// "UPDATE messageData SET longitude = @longitude WHERE message_id = @message_id"
		// cmdU.Connection = conn
		//
		// Dim longitudeUParam As New SqlParameter("@longitude",
		// SqlDbType.NVarChar, 50)
		// longitudeUParam.Value = strLng
		// cmdU.Parameters.Add(longitudeUParam)
		//
		// Dim message_idUParam As New SqlParameter("@message_id",
		// SqlDbType.UniqueIdentifier)
		// Dim gmessage_id As New Guid(mId)
		// message_idUParam.Value = gmessage_id
		// cmdU.Parameters.Add(message_idUParam)
		//
		// Dim previousConnectionStateU As ConnectionState = conn.State
		// Try
		// If conn.State = ConnectionState.Closed Then
		// conn.Open()
		// End If
		// cmdU.ExecuteScalar()
		// Catch ex As SqlException
		// Dim errorMessages As New StringBuilder()
		// Dim i As Integer
		// For i = 0 To ex.Errors.Count - 1
		// errorMessages.Append("<BR> Index #" & i.ToString() &
		// ControlChars.NewLine _
		// & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
		// & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
		// & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
		// & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
		// Next i
		//
		// Finally
		// If previousConnectionStateU = ConnectionState.Closed Then
		// conn.Close()
		// End If
		// End Try
		}
	}

	public void terminateProcessManager() {
		// To be implemented
	}

	public void changeState(ACMessageProcessState s) {
		state = s;
	}

	public void processWhiteSpace(char c) {
		state.ProcessWhiteSpace(c, this);
	}

	public void processLetter(char c) {
		state.ProcessLetter(c, this);
	}

	public void processPunctuation(char c) {
		state.ProcessPunctuation(c, this);
	}

	public void processSymbol(char c) {
		state.ProcessSymbol(c, this);
	}

	public void processNumber(char c) {
		state.ProcessNumber(c, this);
	}

}
